#include "ui.h"
#include "ssd1306.h"
#include "key.h"
#include "cw32l010_systick.h"
#include "rda5807m.h"
unsigned char Page_flag = 0;

void Setting_ui_success(void){
    OLED_ClearArea(page_3x,0,127,63);
    OLED_Show_String(page_3x,24, "SUCCESS", 12, 0);
    OLED_Refresh_GRAM();
    SysTickDelay(800);//800ms
    OLED_ClearArea(page_3x,0,127,63);
}
void Mono_ui_page(uint8_t chose){
    const char *menu_items[] = {"MONOMODE", "STOREMODE"};
    const uint8_t y_positions[] = {0, 12};
    const uint8_t menu_count = 2;
    while (1)
    {
        for (uint8_t i = 0; i < menu_count; i++){
            uint8_t mode = (i + 1 == chose) ? 0 : 1; //反选
            OLED_Show_String(page_3x, y_positions[i], menu_items[i], 12, mode);
            //确认选择
            if(key_info.KEY_FUN && !mode) {
                key_info.KEY_FUN=0;
                if(chose == 1) RDA5807M_Mono(1);//选择1函数
                if(chose == 2) RDA5807M_Mono(0);//选择2函数
                Setting_ui_success();
            }
        }
        if (key_info.KEY_UP){
            key_info.KEY_UP = 0;
            chose ++;
            if (chose > menu_count)
                chose = 1;
        }
        if (key_info.KEY_DOWN){
            key_info.KEY_DOWN = 0;
            chose --;
            if (chose == 0)
                chose = menu_count;
        }
        if (key_info.KEY_RIGHT) key_info.KEY_RIGHT =0;
        // 回到上个界面
        if (key_info.KEY_LEFT){
            key_info.KEY_LEFT = 0;
            OLED_ClearArea(page_3x,0,127,63);
            // Page_flag--;
            break;
        }
        OLED_Refresh_GRAM();
    }
    
}
void Bass_ui_page(uint8_t chose){
    const char *menu_items[] = {"Enable", "Disable"};
    const uint8_t y_positions[] = {0, 12};
    const uint8_t menu_count = 2;
    while (1)
    {
        for (uint8_t i = 0; i < menu_count; i++){
            uint8_t mode = (i + 1 == chose) ? 0 : 1; //反选
            OLED_Show_String(page_3x, y_positions[i], menu_items[i], 12, mode);
            //确认选择
            if(key_info.KEY_FUN && !mode) {
                key_info.KEY_FUN=0;
                if(chose == 1) RDA5807M_Bass(1);//选择1函数
                if(chose == 2) RDA5807M_Bass(0);//选择2函数
                Setting_ui_success();
            }
        }
        if (key_info.KEY_UP){
            key_info.KEY_UP = 0;
            chose ++;
            if (chose >menu_count)
                chose = 1;
        }
        if (key_info.KEY_DOWN){
            key_info.KEY_DOWN = 0;
            chose --;
            if (chose == 0)
                chose = menu_count;
        }
        if (key_info.KEY_RIGHT) key_info.KEY_RIGHT =0;
        // 回到上个界面
        if (key_info.KEY_LEFT){
            key_info.KEY_LEFT = 0;
            OLED_ClearArea(page_3x,0,127,63);//清空区域
            break;
        }
        OLED_Refresh_GRAM();
    }
    
}
// 声音设置页面
void VoiceSet_ui_page(uint8_t chose)
{
    const char *menu_items[] = {"MONO", "BASS"};
    const uint8_t y_positions[] = {0, 12};
    const uint8_t menu_count = 2;
    while (1)
    {
        for (uint8_t i = 0; i < menu_count; i++){
            uint8_t mode = (i + 1 == chose) ? 0 : 1; //反选
            OLED_Show_String(page_2x, y_positions[i], menu_items[i], 12, mode);
            if(key_info.KEY_RIGHT && !mode) { //进入下一级
                key_info.KEY_RIGHT=0;
                if(chose == 1) Mono_ui_page(1);
                if(chose == 2) Bass_ui_page(1);
            }
        }
        if (key_info.KEY_UP){
            key_info.KEY_UP = 0;
            chose ++;
            if (chose > menu_count) chose = 1;
        }
        if (key_info.KEY_DOWN){
            key_info.KEY_DOWN = 0;
            chose --;
            if (chose == 0) chose = menu_count;
        }
        if(key_info.KEY_FUN) key_info.KEY_FUN=0;//清空无用flag
        if (key_info.KEY_LEFT){// 回到上个界面
            key_info.KEY_LEFT = 0;
            OLED_ClearArea(page_2x,0,127,63);//清空区域
            Page_flag--;
            break;
        }
        OLED_Refresh_GRAM();
    }
}
//搜索步进
void FmStep_ui_page(uint8_t chose){
    const char *menu_items[] = {"100Khz", "200Khz","50Khz","25Khz"};
    const uint8_t y_positions[] = {0, 12,24,36};
    const uint8_t menu_count = 4;
    while (1)
    {
        for (uint8_t i = 0; i < menu_count; i++){
            uint8_t mode = (i + 1 == chose) ? 0 : 1; //反选
            OLED_Show_String(page_3x, y_positions[i], menu_items[i], 12, mode);
            //确认选择
            if(key_info.KEY_FUN && !mode) {
                key_info.KEY_FUN=0;
                if(chose == 1) RDA5807M_Step(1);//选择1函数
                if(chose == 2) RDA5807M_Step(0);//选择2函数
                if(chose == 3) RDA5807M_Step(2);//选择3函数
                if(chose == 4) RDA5807M_Step(3);//选择4函数
                Setting_ui_success();
            }
        }
        if (key_info.KEY_UP){
            key_info.KEY_UP = 0;
            chose ++;
            if (chose > menu_count) chose = 1;
        }
        if (key_info.KEY_DOWN){
            key_info.KEY_DOWN = 0;
            chose --;
            if (chose == 0) chose = menu_count;
        }
        if (key_info.KEY_RIGHT) key_info.KEY_RIGHT =0;
        // 回到上个界面
        if (key_info.KEY_LEFT){
            key_info.KEY_LEFT = 0;
            OLED_ClearArea(page_3x,0,127,63);//清空区域
            break;
        }
        OLED_Refresh_GRAM();
    }
    
}
void FmBand_ui_page(uint8_t chose){
    const char *menu_items[] = {"US/EU", "Global"};
    const uint8_t y_positions[] = {0, 12};
    const uint8_t menu_count = 2;
    while (1)
    {
        for (uint8_t i = 0; i < menu_count; i++){
            uint8_t mode = (i + 1 == chose) ? 0 : 1; //反选
            OLED_Show_String(page_3x, y_positions[i], menu_items[i], 12, mode);
            //确认选择
            if(key_info.KEY_FUN && !mode) {
                key_info.KEY_FUN=0;
                if(chose == 1) RDA5807M_band(1) ;//选择1函数
                if(chose == 2) RDA5807M_band(0);//选择2函数
                Setting_ui_success();
            }
        }
        if (key_info.KEY_UP){
            key_info.KEY_UP = 0;
            chose ++;
            if (chose > menu_count)
                chose = 1;
        }
        if (key_info.KEY_DOWN){
            key_info.KEY_DOWN = 0;
            chose --;
            if (chose == 0)
                chose = menu_count;
        }
        if (key_info.KEY_RIGHT) key_info.KEY_RIGHT =0;
        // 回到上个界面
        if (key_info.KEY_LEFT){
            key_info.KEY_LEFT = 0;
            OLED_ClearArea(page_3x,0,127,63);//清空区域
            break;
        }
        OLED_Refresh_GRAM();
    }
    
}
void FmSeekMode_ui_page(uint8_t chose){
    const char *menu_items[] = {"Default", "RSSI"};
    const uint8_t y_positions[] = {0, 12};
    const uint8_t menu_count = 2;
    while (1)
    {
        for (uint8_t i = 0; i < menu_count; i++){
            uint8_t mode = (i + 1 == chose) ? 0 : 1; //反选
            OLED_Show_String(page_3x, y_positions[i], menu_items[i], 12, mode);
            //确认选择
            if(key_info.KEY_FUN && !mode) {
                key_info.KEY_FUN=0;
                if(chose == 1) RDA5807M_SeekMode(0);//选择1函数
                if(chose == 2) RDA5807M_SeekMode(1);//选择2函数
                Setting_ui_success();
            }
        }
        if (key_info.KEY_UP){
            key_info.KEY_UP = 0;
            chose ++;
            if (chose >menu_count) chose = 1;
        }
        if (key_info.KEY_DOWN){
            key_info.KEY_DOWN = 0;
            chose --;
            if (chose == 0) chose = menu_count;
        }
        if (key_info.KEY_RIGHT) key_info.KEY_RIGHT =0;
        // 回到上个界面
        if (key_info.KEY_LEFT){
            key_info.KEY_LEFT = 0;
            OLED_ClearArea(page_3x,0,127,63);//清空区域
            break;
        }
        OLED_Refresh_GRAM();
    }
}
void FmLnaMode_ui_page(uint8_t chose){
    const char *menu_items[] = {"LNAP", "LNAN","DUAL"};
    const uint8_t y_positions[] = {0, 12,24};
    const uint8_t menu_count = 3;
    while (1)
    {
        for (uint8_t i = 0; i < menu_count; i++){
            uint8_t mode = (i + 1 == chose) ? 0 : 1; //反选
            OLED_Show_String(page_3x, y_positions[i], menu_items[i], 12, mode);
            //确认选择
            if(key_info.KEY_FUN && !mode) {
                key_info.KEY_FUN=0;
                if(chose == 1) RDA5807M_LnaMode(1);//选择1函数
                if(chose == 2) RDA5807M_LnaMode(0);//选择2函数
                if(chose == 3) RDA5807M_LnaMode(2);//选择3函数
                Setting_ui_success();
            }
        }
        if (key_info.KEY_UP){
            key_info.KEY_UP = 0;
            chose ++;
            if (chose >menu_count) chose = 1;
        }
        if (key_info.KEY_DOWN){
            key_info.KEY_DOWN = 0;
            chose --;
            if (chose == 0) chose = menu_count;
        }
        if (key_info.KEY_RIGHT) key_info.KEY_RIGHT =0;
        // 回到上个界面
        if (key_info.KEY_LEFT){
            key_info.KEY_LEFT = 0;
            OLED_ClearArea(page_3x,0,127,63);//清空区域
            break;
        }
        OLED_Refresh_GRAM();
    }
}
void FmLnaCurrent_ui_page(uint8_t chose){
    const char *menu_items[] = {"1.8ma", "2.1ma","2.5ma","3.0ma"};
    const uint8_t y_positions[] = {0, 12,24,36};
    const uint8_t menu_count = 4;
    while (1)
    {
        for (uint8_t i = 0; i < menu_count; i++){
            uint8_t mode = (i + 1 == chose) ? 0 : 1; //反选
            OLED_Show_String(page_3x, y_positions[i], menu_items[i], 12, mode);
            //确认选择
            if(key_info.KEY_FUN && !mode) {
                key_info.KEY_FUN=0;
                if(chose == 1) RDA5807M_LnaCurrent(0);//选择1函数
                if(chose == 2) RDA5807M_LnaCurrent(1);//选择2函数
                if(chose == 3) RDA5807M_LnaCurrent(2);//选择3函数
                if(chose == 4) RDA5807M_LnaCurrent(3);//选择4函数
                Setting_ui_success();
            }
        }
        if (key_info.KEY_UP){
            key_info.KEY_UP = 0;
            chose ++;
            if (chose >menu_count) chose = 1;
        }
        if (key_info.KEY_DOWN){
            key_info.KEY_DOWN = 0;
            chose --;
            if (chose == 0) chose = menu_count;
        }
        if (key_info.KEY_RIGHT) key_info.KEY_RIGHT =0;
        // 回到上个界面
        if (key_info.KEY_LEFT){
            key_info.KEY_LEFT = 0;
            OLED_ClearArea(page_3x,0,127,63);//清空区域
            break;
        }
        OLED_Refresh_GRAM();
    }
}

// RX设置页面
void RxSet_ui_page(uint8_t chose)
{
    const char *menu_items[] = {"STEP", "BAND","SEEKM","LANMO","LANCU"};
    const uint8_t y_positions[] = {0, 12,24,36,48};
    const uint8_t menu_count = 5;
    while (1)
    {
        for (uint8_t i = 0; i < menu_count; i++){
            uint8_t mode = (i + 1 == chose) ? 0 : 1; //反选
            OLED_Show_String(page_2x, y_positions[i], menu_items[i], 12, mode);
            if(key_info.KEY_RIGHT && !mode) { //进入下一级
                key_info.KEY_RIGHT=0;
                if(chose == 1) FmStep_ui_page(1);
                if(chose == 2) FmBand_ui_page(1);
                if(chose == 3) FmSeekMode_ui_page(1);
                if(chose == 4) FmLnaMode_ui_page(1);
                if(chose == 5) FmLnaCurrent_ui_page(1);
            }
        }
        if (key_info.KEY_UP){
            key_info.KEY_UP = 0;
            chose ++;
            if (chose > menu_count) chose = 1;
        }
        if (key_info.KEY_DOWN){
            key_info.KEY_DOWN = 0;
            chose --;
            if (chose == 0) chose = menu_count;
        }
        if(key_info.KEY_FUN) key_info.KEY_FUN=0;//清空无用flag
        if (key_info.KEY_LEFT){// 回到上个界面
            key_info.KEY_LEFT = 0;
            OLED_ClearArea(page_2x,0,127,63);//清空区域
            Page_flag--;
            break;
        }
        OLED_Refresh_GRAM();
    }
}
// 时间设置页面
void TimeSet_ui_page(uint8_t chose)
{
    const char *menu_items[] = {"DATE", "TIME"};
    const uint8_t y_positions[] = {0, 12};
    const uint8_t menu_count = 2;
    while (1)
    {
        for (uint8_t i = 0; i < menu_count; i++){
            uint8_t mode = (i + 1 == chose) ? 0 : 1; //反选
            OLED_Show_String(page_2x, y_positions[i], menu_items[i], 12, mode);
            if(key_info.KEY_RIGHT && !mode) { //进入下一级
                key_info.KEY_RIGHT=0;
                if(chose == 1) ;//日期设置ui
                if(chose == 2) ;//时间设置ui
            }
        }
        if (key_info.KEY_UP){
            key_info.KEY_UP = 0;
            chose ++;
            if (chose > menu_count) chose = 1;
        }
        if (key_info.KEY_DOWN){
            key_info.KEY_DOWN = 0;
            chose --;
            if (chose == 0) chose = menu_count;
        }
        if(key_info.KEY_FUN) key_info.KEY_FUN=0;//清空无用flag
        if (key_info.KEY_LEFT){// 回到上个界面
            key_info.KEY_LEFT = 0;
            OLED_ClearArea(page_2x,0,127,63);//清空区域
            Page_flag--;
            break;
        }
        OLED_Refresh_GRAM();
    }
}
void DeviceInfo_ui_page(void){
    OLED_Show_String(page_2x, 0, "MCU:CW32L010", 12, 1);
    OLED_Show_String(page_2x, 12, "FLASH:64Kb", 12, 1);
    OLED_Show_String(page_2x, 24, "RAM:4Kb", 12, 1);
    OLED_Show_String(page_2x, 36, "SSD1306_128x64", 12, 1);
    OLED_Show_String(page_2x, 48, "Radio:RAD5807M", 12, 1);
    OLED_Refresh_GRAM();
    while (1)
    {
        if (key_info.KEY_UP) key_info.KEY_UP = 0;
        if (key_info.KEY_DOWN) key_info.KEY_DOWN = 0;
        if(key_info.KEY_FUN) key_info.KEY_FUN=0;//清空无用flag
        if (key_info.KEY_LEFT){// 回到上个界面
            key_info.KEY_LEFT = 0;
            OLED_ClearArea(page_2x,0,127,63);//清空区域
            Page_flag--;
            break;
        }
    }
}

// 主界面
void Main_ui_page(void)
{
    OLED_Clear();
    OLED_TIME();
    OLED_FM();
    Key_Process();
    OLED_Refresh_GRAM();
}
// 设置界面，目前设想
void Setting_ui_page(uint8_t chose)
{
    const char *menu_items[] = {"VOICE", "RXSET","TIME","INFO"};
    const uint8_t y_positions[] = {0, 12,24,36};
    const uint8_t menu_count = 4;
    OLED_Clear();
    while (1)
    {
        for (uint8_t i = 0; i < menu_count; i++){
            uint8_t mode = (i + 1 == chose) ? 0 : 1; //反选
            OLED_Show_String(0, y_positions[i], menu_items[i], 12, mode);
            //进入下一级
            if(key_info.KEY_RIGHT && !mode) {
                key_info.KEY_RIGHT=0;
                if(chose == 1) VoiceSet_ui_page(1);
                if(chose == 2) RxSet_ui_page(1);
                if(chose == 3) TimeSet_ui_page(1);
                if(chose == 4) DeviceInfo_ui_page();
            }
        }
        // 回到上个界面
        if (key_info.KEY_LEFT)
        {
            key_info.KEY_LEFT = 0;
            Page_flag=0;
            OLED_Clear();
            break;
        }
        if (key_info.KEY_UP)
        {
            key_info.KEY_UP = 0;
            chose ++;
            if (chose > menu_count) chose = 1;
        }
        if (key_info.KEY_DOWN)
        {
            key_info.KEY_DOWN = 0;
            chose --;
            if (chose == 0) chose = menu_count;
        }
        if(key_info.KEY_FUN) key_info.KEY_FUN=0;//清空无用flag
        OLED_Refresh_GRAM();
    }
}
void ui_process(void)
{
    if (key_info.KEY_FUN)
    {
        key_info.KEY_FUN = 0;
        Page_flag = 1;
    }
    switch (Page_flag)
    {
    // 主界面
    case 0:
        Main_ui_page();
        break;
    // 设置界面
    case 1:
        Setting_ui_page(1);
        /* code */
        break;
    case 2:
        /* code */
        break;
    default:
        break;
    }
}